package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;

//新增
    @Override
    @Transactional
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //1. 添加检查组对象， 回显主键id
        roleDao.add(role);
        //2. 维护中间表的数据（检查组和检查项的关系）
        if(role.getId() != null && permissionIds != null && permissionIds.length > 0 && menuIds != null && menuIds.length > 0){
            for (Integer permissionId : permissionIds) {
                roleDao.setp(role.getId() ,permissionId );
            }
            for (Integer menuId : menuIds) {
                roleDao.setm(role.getId() ,menuId );
            }
        }
    }

    //分页查询
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            Page<CheckGroup> page = roleDao.findByRole(queryPageBean.getQueryString());
            log.debug("page{"+page+"}");
            return new PageResult(page.getTotal(), page);
    }

    //角色删除
    @Override
    @Transactional
    public void delById(Integer id) {
        long count = roleDao.findPermById(id);
        long counts = roleDao.findMenuById(id);

        //删除与菜单关系
        roleDao.delM(id);
        //删除与权限关系
        roleDao.delP(id);

        //删除检查组
        roleDao.delById(id);
    }


    //回显角色查询
    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findpermissionIdsById(Integer id) {
        return roleDao.findpermissionIdsById(id);
    }

    @Override
    public List<Integer> findmenuIdsById(Integer id) {
        return roleDao.findmenuIdsById(id);
    }

    @Transactional
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //1.修改角色
        roleDao.edit(role);
        //2.1 先删除该角色与权限的关系
        roleDao.delRelationP(role.getId());
        //2.1 删除该角色与菜单的关系
        roleDao.delRelationM(role.getId());
        //2.2 添加新的关系
        if(role.getId() != null && permissionIds != null && permissionIds.length > 0 && menuIds != null && menuIds.length > 0){
            for (Integer permissionId : permissionIds) {
                roleDao.setp(role.getId() ,permissionId );
            }
            for (Integer menuId : menuIds) {
                roleDao.setm(role.getId() ,menuId );
            }
        }
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

}
